<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Telecom_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_telecom';
	}
        
        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),
				array(
						'name' => 'username',
						'label' => $this->lang->line('username'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'phone',
						'label' => $this->lang->line('phone'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
				array(
						'name' 	=> 'message',
						'label' => $this->lang->line('message'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'ipaddress',
						'label' => $this->lang->line('ipaddress'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'code',
						'label' => $this->lang->line('code'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'status',
						'label' => $this->lang->line('status'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label'  => $right['add']==TRUE?'<div class="btn-group">
						<a style="width: 82px;" href="'.site_url('auth/telecom/sendsms').'" class="btn btn btn-success">'.$this->lang->line('send').'</a>
						</div>':'',
						'searchoptions' => FALSE
				)
		);
	}
	
	public function json_data($controller, $right){
            
                if($this->session->userdata('h_telecom')){
                    $arr_status = json_decode($this->session->userdata('h_telecom'));
                }
                
		$this->datatables
		->select("tbl.id,tbl.phone,tbl.message,tbl.ipaddress,tbl.code,tbl.status,tbl.lastupdated,usr.username AS username,agent.pcc AS pcc")
		->from($this->table_name. ' AS tbl')
                ->join('users AS usr','usr.id=tbl.userid')
                ->join('book_agent_code AS agent','agent.id = tbl.agent_code');
                
                if($this->session->userdata['group_id']!=1){
                    $this->datatables->where('tbl.userid = '.$this->session->userdata['user_id']);
                }
                
                if(isset($arr_status) && $arr_status->phone != ''){
                    $this->datatables->where('tbl.phone',$arr_status->phone);
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                $count = 1;
                
                $this->session->unset_userdata('h_telecom');
                
		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
					$count++,
                                        $item['username'],
					$item['phone'],
                            		$item['message'],
                                        $item['ipaddress'],
                                        $item['code'],
                                        ($item['status']==0 && $item['code']==0)?$this->lang->line('fail'):$this->lang->line('success'),
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated'])),
                                        $item['pcc'],
					$this->add_button($controller, $right, $item),
			);
		}
	
		return json_encode($ouput);
	}

}